# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time

  include  AuthenticatedSystem

  DEFAULT_LOCALE = 'zh'

  around_filter :set_language

  private
  def set_language
    Gibberish.use_language(query_locale) { yield }
  end

  def query_locale
    session[:locale] = params[:locale] || session[:locale] ||
      request_language || DEFAULT_LOCALE
  end

  def request_language
    request.env['HTTP_ACCEPT_LANGUAGE'] and  request.env['HTTP_ACCEPT_LANGUAGE'][/[^,;]+/]
  end
end
